//
//  CVCellOfFMCollect.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/23.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "CVCellOfFMCollect.h"
// models
#import "DataOfFMCollect.h"
#import "FavoriteOfDataFMCollect.h"
#import "RootClassOfFMCollect.h"
// 基类
#import "BaseTableView.h"
// 解析工具
#import "ZWYNetTool.h"
// 一样的cell, 就拿过来用了
#import "TVCellOfSecondRowListenFM.h"
//
#import "MJMacro.h"
// sd
#import "UIImageView+WebCache.h"
// 播放的界面
#import "VCOfListenFMPlayInterface.h"
// 当前所在的 VC
#import "VCOfCollec.h"
@interface CVCellOfFMCollect()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) BaseTableView *tableViewOfCollect;
@property (nonatomic, strong) RootClassOfFMCollect *rootClassOfFMCollect;
@end
@implementation CVCellOfFMCollect
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
        [self handleDataTestCollect];
    }
    return self;
}
#pragma mark - 收藏测试题的, 数据解析
- (void)handleDataTestCollect{
    [ZWYNetTool GET:@"http://yiapi.xinli001.com/v2/yi/my-newfavorite-list.json?key=18ee0593a01c07f71d28fa936d671457&_platform=ios&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93&type=fm" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        NSLog(@"%@", result);
        _rootClassOfFMCollect = [RootClassOfFMCollect modelDic:result];
        [self.tableViewOfCollect reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
#pragma mark - 创建 tableView
- (void)createTableView{
    self.tableViewOfCollect = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.contentView addSubview:_tableViewOfCollect];
    self.tableViewOfCollect.delegate = self;
    self.tableViewOfCollect.dataSource = self;
    [self.tableViewOfCollect registerNib:[UINib nibWithNibName:@"TVCellOfSecondRowListenFM" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfSecondRowListenFM"];
}
#pragma mark - tabelView 代理方法 row 的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _rootClassOfFMCollect.data.favorite.count;
}
#pragma mark - 返回每个 row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT / 6;
}
#pragma mark - 分区头的文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@" 一共收藏了 %@ 个电台音乐", _rootClassOfFMCollect.data.num];
}
#pragma mark - TableView 代理方法 cell 的赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TVCellOfSecondRowListenFM *cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfSecondRowListenFM" forIndexPath:indexPath];
    [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:_rootClassOfFMCollect.data.favorite[indexPath.row].cover] placeholderImage:[UIImage imageNamed:@"0"]];
    cell.titleLabel.text = _rootClassOfFMCollect.data.favorite[indexPath.row].title;
    cell.speakLabel.text = _rootClassOfFMCollect.data.favorite[indexPath.row].speak;
    cell.viewnumLabel.text = _rootClassOfFMCollect.data.favorite[indexPath.row].viewnum;
    return cell;
}
#pragma mark - tableView 的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    VCOfListenFMPlayInterface *vcOfListenFMPlayInterface = [VCOfListenFMPlayInterface defaultManager];
    // 歌曲下一个角标的确定
    vcOfListenFMPlayInterface.numberOfSelectNextSong = indexPath.row + 1;
    // 歌曲下一个角标的确定
    vcOfListenFMPlayInterface.numberOfSelectLastSong = indexPath.row - 1;
    // 传递 model
//    vcOfListenFMPlayInterface.clickButtonInListenFMRootClass = _clickButtonInListenFMRootClass;
    // 传递这些属性
    vcOfListenFMPlayInterface.urlString = _rootClassOfFMCollect.data.favorite[indexPath.row].url;
    vcOfListenFMPlayInterface.coverString = _rootClassOfFMCollect.data.favorite[indexPath.row].cover;
    vcOfListenFMPlayInterface.titleString = _rootClassOfFMCollect.data.favorite[indexPath.row].title;
    vcOfListenFMPlayInterface.speakString = _rootClassOfFMCollect.data.favorite[indexPath.row].speak;
    // 界面跳转
    VCOfCollec *vcOfCollec = (VCOfCollec *)self.nextResponder.nextResponder.nextResponder;
    [vcOfCollec presentViewController:vcOfListenFMPlayInterface animated:YES completion:^{
        
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
